<template>
  <el-dialog :title="title" :visible.sync="visible" width="40%" :before-close="handleClose">
    <el-form ref="form" :model="form" status-icon :rules="rules" v-loading="loading">
      <el-form-item label="账户" :label-width="formLabelWidth" prop="username">
        <el-input v-model="form.username" autocomplete="off" placeholder="请输入账户"></el-input>
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
        <el-input v-model="form.password" autocomplete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth">
        <el-radio-group v-model="form.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系电话" :label-width="formLabelWidth" prop="mobile">
        <el-input v-model="form.mobile" autocomplete="off" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
        <el-input type="textarea" v-model="form.remark" autocomplete="off" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="角色" :label-width="formLabelWidth" prop="roles">
        <el-checkbox-group v-model="form.roles">
          <el-checkbox v-for="item in roles" :label="item.id" :key="item.id">{{item.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listRole } from "@/api/role";
import { addUser, editUser } from "@/api/user";

export default {
  props: {
    // dialog标题
    title: {
      type: String,
      default: "",
    },
    visible: {
      type: Boolean,
      default: true,
    },
    // 表单
    form: {
      type: Object,
      default: {},
    },
    // 用于关闭窗口
    removeClose: Function,
  },
  watch: {
    visible(value) {
      if (value) {
        this.getRoles();
      }
    },
  },
  data() {
    return {
      // 表单校验
      rules: {
      },
      formLabelWidth: "80px",
      roles: [],
      loading: false
    };
  },
  methods: {
    // 关闭窗口
    handleClose() {
      this.$refs["form"].resetFields(); // 表单清空
      this.removeClose(); // 调用父组件传递的方法
    },
    // 获取所有角色
    getRoles() {
      const params = {
        model: "all",
        status: 1,
      };
      listRole(params).then((response) => {
        if (response.code === 1) {
          this.roles = response.data;
        } else {
          this.$message({
            message: response.message,
            type: "error",
          });
        }
      });
    },
    // 提交表单
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        // 表单校验
        if (!valid) {
          return false;
        }
        // 提交数据
        this.submitData();
      });
    },
    // 异步提交表单
    async submitData() {
      let response = null;
      console.log(this.form);
      if (!this.form.id) {
        // 新增
        response = await addUser(this.form);
      } else {
        // 更新
        response = await editUser(this.form.id, this.form);
      }
      this.$notify({
        title: response.message,
        message: response.message,
        type: response.code == 1 ? "success" : "error",
        duration: 2000,
      });
      if (response.code === 1) {
        // 关闭窗口
        this.handleClose();
      } else {
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>